﻿using KAsFrame.Common.Dto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KAsFrame.Common.Filter
{
    /// <summary>
    /// 异常过滤器
    /// </summary>
    /// <remarks>
    /// 异常集中处理，构建对象返回
    /// </remarks>
    public class ExceptionFilter : IAsyncExceptionFilter
    {
        public Task OnExceptionAsync(ExceptionContext context)
        {
            if (context.ExceptionHandled == false)
            {
                context.Result = new JsonResult(ApiResult<object>.IsError(context.Exception.Message));
            }
            context.ExceptionHandled = true;

            return Task.CompletedTask;
        }
    }
}